package com.itheima.mall.api.pms;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.dto.ProductAttributeDto;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.mapper.pms.ProductAttributeCategoryMapper;
import com.itheima.mall.mapper.pms.ProductAttributeMapper;
import com.itheima.mall.mapper.pms.ProductMapper;
import com.itheima.mall.pojo.pms.Product;
import com.itheima.mall.pojo.pms.ProductAttribute;
import com.itheima.mall.pojo.pms.ProductAttributeCategory;
import com.itheima.mall.vo.AttrInfoVo;
import com.itheima.mall.vo.PageResult;
import com.itheima.mall.vo.ProductAttributeCategoryVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class ProductAttributeServiceApiImpl implements ProductAttributeServiceApi {
    @Autowired
    private ProductAttributeCategoryMapper productAttributeCategoryMapper;

    @Autowired
    private ProductAttributeMapper productAttributeMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Boolean categoryCreate(String name) {
        LambdaQueryWrapper<ProductAttributeCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotEmpty(name),ProductAttributeCategory::getName,name);
        ProductAttributeCategory productAttributeCategory1 = productAttributeCategoryMapper.selectOne(queryWrapper);
        if(productAttributeCategory1 != null){
            return false;
        }else {
            ProductAttributeCategory productAttributeCategory = new ProductAttributeCategory();
            productAttributeCategory.setName(name);
            productAttributeCategory.setAttributeCount(0);
            productAttributeCategory.setParamCount(0);
            productAttributeCategoryMapper.insert(productAttributeCategory);
            return true;
        }
    }

    @Override
    public Boolean categoryDelete(Long categoryId) {
        Boolean flag = true;
        LambdaQueryWrapper<Product> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Product::getProductAttributeCategoryId,categoryId);
        List<Product> products = productMapper.selectList(queryWrapper1);
        if(products.size() != 0){
            flag = false;
        }else {
            LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductAttribute::getProductAttributeCategoryId,categoryId);
            productAttributeMapper.delete(queryWrapper);
            LambdaQueryWrapper<ProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProductAttributeCategory::getId,categoryId);
            productAttributeCategoryMapper.delete(wrapper);
        }
        return flag;
    }

    @Override
    public PageResult categoryList(Integer pageNum, Integer pageSize) {
        PageResult result = new PageResult();
        Page<ProductAttributeCategory> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<ProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(ProductAttributeCategory::getId);
        Page<ProductAttributeCategory> productAttributeCategoryPage = productAttributeCategoryMapper.selectPage(pageInfo, wrapper);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(productAttributeCategoryPage.getRecords());
        result.setTotalPage(Convert.toInt(productAttributeCategoryPage.getPages()));
        result.setTotal(Convert.toInt(productAttributeCategoryPage.getTotal()));
        return result;
    }

    @Override
    public List<ProductAttributeCategoryVo> categoryListWithAttr(){
        LambdaQueryWrapper<ProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(ProductAttributeCategory::getId);
        List<ProductAttributeCategory> categoryList = productAttributeCategoryMapper.selectList(wrapper);
        List<ProductAttributeCategoryVo> list = new ArrayList<>();
        for (ProductAttributeCategory productAttributeCategory : categoryList) {
            ProductAttributeCategoryVo vo = new ProductAttributeCategoryVo();
            BeanUtil.copyProperties(productAttributeCategory,vo);
            LambdaQueryWrapper<ProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductAttribute::getProductAttributeCategoryId,productAttributeCategory.getId());
            List<ProductAttribute> productAttributeList = productAttributeMapper.selectList(queryWrapper);
            vo.setProductAttributeList(productAttributeList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public ProductAttributeCategory selectCategory(Long categoryId) {
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectById(categoryId);
        return productAttributeCategory;
    }

    @Override
    public void categoryUpdate(Long categoryId, String name) {
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectById(categoryId);
        productAttributeCategory.setName(name);
        productAttributeCategoryMapper.updateById(productAttributeCategory);
    }

    @Override
    public void create(ProductAttributeDto productAttributeDto) {
        LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductAttribute::getName,productAttributeDto.getName());
        wrapper.eq(ProductAttribute::getProductAttributeCategoryId,productAttributeDto.getProductAttributeCategoryId());
        ProductAttribute selectOne = productAttributeMapper.selectOne(wrapper);
        if (selectOne != null){
            throw new BusinessException("该商品属性已存在,添加失败");
        }
        ProductAttribute productAttribute = new ProductAttribute();
        BeanUtil.copyProperties(productAttributeDto,productAttribute);
        Long id = Convert.toLong(productAttributeDto.getProductAttributeCategoryId());
        productAttribute.setProductAttributeCategoryId(id);
        productAttributeMapper.insert(productAttribute);
        ProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectById(id);
        Integer attributeCount = productAttributeCategory.getAttributeCount();
        Integer paramCount = productAttributeCategory.getParamCount();
        if (productAttributeDto.getType()==0){
            productAttributeCategory.setAttributeCount(attributeCount+1);
            productAttributeCategoryMapper.updateById(productAttributeCategory);
        }
        if (productAttributeDto.getType()==1){
            productAttributeCategory.setParamCount(paramCount+1);
            productAttributeCategoryMapper.updateById(productAttributeCategory);
        }
        
    }

    @Override
    public void delete(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException("未获取到ID");
        }
        for (Integer id : ids) {
            Long productAttributeId = Convert.toLong(id);
            ProductAttribute productAttribute = productAttributeMapper.selectById(productAttributeId);
            ProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectById(productAttribute.getProductAttributeCategoryId());
            if (productAttributeCategory!=null){
                Integer attributeCount = productAttributeCategory.getAttributeCount();
                Integer paramCount = productAttributeCategory.getParamCount();
                if (productAttribute.getType()==0){
                    productAttributeCategory.setAttributeCount(attributeCount-1);
                    productAttributeCategoryMapper.updateById(productAttributeCategory);
                }
                if (productAttribute.getType()==1){
                    productAttributeCategory.setParamCount(paramCount-1);
                    productAttributeCategoryMapper.updateById(productAttributeCategory);
                }
            }
            LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProductAttribute::getId,productAttributeId);
            productAttributeMapper.delete(wrapper);
        }
    }

    @Override
    public ProductAttribute findById(Long productAttributeId) {
        ProductAttribute productAttribute = productAttributeMapper.selectById(productAttributeId);
        return productAttribute;
    }

    @Override
    public void update(Long productAttributeId, ProductAttribute productAttribute) {
        LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductAttribute::getId,productAttributeId);
        productAttributeMapper.update(productAttribute,wrapper);
    }

    @Override
    public PageResult listCid(Integer cid, Integer pageNum, Integer pageSize, Integer type) {
        PageResult result = new PageResult();
        Page<ProductAttribute> pagInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductAttribute::getProductAttributeCategoryId,cid);
        wrapper.eq(ProductAttribute::getType,type);
        Page<ProductAttribute> productAttributePage = productAttributeMapper.selectPage(pagInfo, wrapper);
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setList(productAttributePage.getRecords());
        result.setTotalPage(Convert.toInt(productAttributePage.getPages()));
        result.setTotal(Convert.toInt(productAttributePage.getTotal()));
        return result;
    }

    @Override
    public List<AttrInfoVo> attrInfo(Long id) {
        LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductAttribute::getProductAttributeCategoryId,id);
        List<ProductAttribute> productAttributeList = productAttributeMapper.selectList(wrapper);
        List<AttrInfoVo> list = new ArrayList<>();
        for (ProductAttribute productAttribute : productAttributeList) {
            AttrInfoVo vo = new AttrInfoVo();
            vo.setAttributeId(Convert.toInt(productAttribute.getId()));
            vo.setAttributeCategoryId(productAttribute.getType());
            list.add(vo);
        }
        return list;
    }
}
